$(function() {
	var rows_delete=0;
	var mode="add";
	var m_id=0;
    var url='controls/student_register_model.php';
	var cdt = new Date();  
	var current_year =cdt.getFullYear();
	var curr_month = cdt.getMonth()+1;
	var ar;
   
	var checkDate = $('.btndate').datepicker().on('changeDate', function(ev) {
		//checkDate.hide();
		var _bdate = $("#txtBirthDate").val();
		if (_bdate!=""){
			var str_Split = _bdate.split('/');
			
			$("#txtAge").val(parseInt(current_year) - parseInt(str_Split[2])) ;
			$("#txtAgeMonth").val(parseInt(curr_month) - parseInt(str_Split[1])) ;
		}
	}).data('datepicker');
	
	$("#txtOtherParentStatus,#txtBus_fee").attr('disabled','disabled');
	
	$("input[name='rdoParentStatus']").click(function(){
		if($('input:radio[name=rdoParentStatus]:checked').val() == "O"){
			$("#txtOtherParentStatus").removeAttr('disabled');
			$("#txtOtherParentStatus").focus();
		}else{
			$("#txtOtherParentStatus").attr('disabled','disabled');
		}
	});
	
	$("input:radio[name='rdoBus']").click(function(){
	 
		if($('input:radio[name=rdoBus]:checked').val() == "Y"){
			$("#txtBus_fee").removeAttr('disabled');
			$("#txtBus_fee").focus();
		}else{
			$("#txtBus_fee").attr('disabled','disabled');
		}
	});
	
	
	
	 $("input[name=number]").live('keypress',function(e){

		if( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)){
			return false;
		}
	});
	
	$.get(url, {"m":"class"},function(data){
			var obj=eval('('+data+')');	
			if (obj != null){
				$.each(obj, function(index, text) {
					$("#cboClass").append(
							$('<option></option>').val(text).html(index)
						 );
				});
			}
	});
	$.get(url, {"m":"term"},function(data){
			var obj=eval('('+data+')');	
			if (obj != null){
				$.each(obj, function(index, text) {
					$("#cboTerm").append(
							$('<option></option>').val(text).html(index)
						 );
				});
			}
	});
	$.get(url, {"m":"assets"},function(data){
			var obj=eval('('+data+')');	
			if (obj != null){
				$.each(obj, function(index, text) {
					$("#cboAssest").append(
							$('<option></option>').val(text).html(index)
						 );
				});
			}
			CalcTotal();
	});
     
	$("#cboAssest,#cboTerm").change(function(){
		CalcTotal();
	});
	 
	$("#txtOtherpay1,#txtOtherpay2,#txtStudent_form_fee,#txtBus_fee").live('keyup',function(e){
		CalcTotal();
	});
	function CalcTotal(){
 
		var _iasset = parseInt(isNaN($("#cboAssest option:selected").text())?0:$("#cboAssest option:selected").text());
		var _term = parseInt(isNaN($("#cboTerm option:selected").text())?0:$("#cboTerm option:selected").text());
		var _other1= parseInt(isNaN($("#txtOtherpay1").val())?0:$("#txtOtherpay1").val());
		var _other2= parseInt(isNaN($("#txtOtherpay2").val())?0:$("#txtOtherpay2").val());
		var _form = parseInt(isNaN($("#txtStudent_form_fee").val())?0:$("#txtStudent_form_fee").val());
		var _bus = parseInt(isNaN($("#txtBus_fee").val())?0:$("#txtBus_fee").val());
		var _total =  parseInt(_iasset)+ parseInt(_term )+ parseInt(_other1 )+ parseInt(_other2) + parseInt(_form);
		 
		if ($('input:radio[name=rdoBus]:checked').val() == "Y"){
			_total += _bus;
		}
	  
		$("#txtTotal").val(CommaFormatted(_total.toFixed(2)));
		
	}
	
	$("#btnSave").click(function(){
		$("input").not("[type=submit]").jqBootstrapValidation({preventSubmit: false,
			submitSuccess: function ($form, event) { event.preventDefault();}
		}); 
	  
		var _thai_name = $.trim($("#txtThai_name").val());		
		var _thai_surname = $.trim($("#txtThai_surname").val());	
		var _thai_nick_name =$.trim($("#txtThai_nickname").val());
		
		var _name = $.trim($("#txtEng_name").val());	
		var _surname = $.trim($("#txtEng_surname").val());
		var _nickname = $.trim($("#txtEng_nickname").val());		
		var _sex = $("input:radio[name=rdoSex]:checked").val();
		var _birthdate = $("#txtBirthDate").val();
		
		var _term = $("#cboTerm").val();
		var _assets= $("#cboAssest").val();
		var _class = $("#cboClass").val();
		var _registerdate=$("#txtRegisterDate").val();
		if (_thai_name.length !=0 && _thai_surname.length !=0){
			
			ar={"thai_name":_thai_name
				, "thai_surname":_thai_surname 
				, "thai_nickname":_thai_nick_name
				, "eng_name":_name
				, "eng_surname":_surname
				, "eng_nickname":_nickname
				, "sex":_sex
				, "birthdate":_birthdate
				, 'free_term':_term
				, 'free_assetment':_assets
				, 'init_class':_class
				, 'registerdate':_registerdate
				,'religion':$("#txtReligion").val()
				,'race':$("#txtRace").val()
				,'nationality':$("#txtNationality").val()
				,'parent_status':$("input:radio[name=rdoParentStatus]").val()
				,'parent_status_remak':$("#txtOtherParentStatus").val()
				,'father_name':$("#txtFather_name").val()
				,'father_surname':$("#txtFather_surname").val()
				,'father_age':$("#txtFather_age").val()
				,'father_education':$("#txtFather_education").val()
			
				,'father_occupation':$("#txtFather_occupation").val()
				,'father_occupation_info':$("#txtFather_occupation_info").val()
				,'father_position':$("#txtFather_position").val()
				,'father_company':$("#txtFather_company").val()
				,'father_company_address':$("#txtFather_company_address").val()
				
				,'father_province':$("#txtFather_province").val()
				,'father_zipcode':$("#txtFather_zipcode").val()
				,'father_tele':$("#txtFather_tele").val()
				,'father_fax':$("#txtFather_fax").val()
				
				,'mather_name':$("#txtMather_name").val()
				,'mather_surname':$("#txtMather_surname").val()
				,'mather_age':$("#txtMather_age").val()
				,'mather_education':$("#txtMather_education").val()
				,'mather_position':$("#txtMather_position").val()
				,'mather_occupation':$("#txtMather_occupation").val()
				,'mather_occupation_info':$("#txtMather_detail").val()
				
				,'mather_company':$("#txtMather_company").val()
				,'mather_company_adress':$("#txtMather_company_address").val()
				,'mather_province':$("#txtMather_province").val()
				,'mather_zipcode':$("#txtMather_zipcode").val()
				,'mather_tele':$("#txtMather_tele").val()
				,'mather_fax':$("#txtMather_fax").val()
			
				,'address':$("#txtAddress").val()
				,'privince':$("#txtProvince").val()
				,'zipcode':$("#txtZipcode").val()
				,'telephone':$("#txtTelephone").val()
				,'mobilephone':$("#txtMobilephone").val()
				,'email':$("#txtEmail").val() || ''
				
				,'emergency_contact':$("#txtContact").val()
				,'no_of_brother1':$("#txtNo_of_brother1").val() || '0'
				,'brother_1':$("#txtBrother_1").val() || '0'
				,'brother_2':$("#txtBrother_2").val() || '0'
				,'brother_3':$("#txtBrother_3").val() || '0'
				,'no_of_brother2':$("#txtNo_of_brother2").val() || '0'
				,'brother_4':$("#txtBrother_4").val() || '0'
				,'brother_5':$("#txtBrother_5").val() || '0'
				,'brother_6':$("#txtBrother_6").val() || '0'
				
				,'blood':$("#txtBlood").val()
				,'food_allergies':$("#txtFood_allergies").val()
				,'be_allergic':$("#txtBe_allergic").val()
				
				,'congenital_disease':$("#txtCongenital_disease").val()
				,'special_character':$("#txtSpecial_character").val()
			 
				,'free_other1':$("#txtOtherpay1").val() || '0'
				,'free_other2':$("#txtOtherpay2").val() || '0'
				
				,'bus':$('input:radio[name=rdoBus]:checked').val()
				,'bus_payment': $("#txtBus_fee").val()  
				,'free_student_form': $("#txtStudent_form_fee").val()
				,'suggessted': $("#txtRecommance").val()
				,'doc_idcard': $("input[name=doc_idcard]:checked").val()=='Y'?'Y':'N'
				,'doc_address':$("input[name=doc_address]:checked").val()=='Y'?'Y':'N'
				,'doc_birth': $("input[name=doc_birth]:checked").val()=='Y'?'Y':'N'
				,'doc_pic_chident': $("input[name=doc_pic_chident]:checked").val()=='Y'?'Y':'N'  
				,'doc_pic_parent':$("input[name=doc_pic_parent]:checked").val()=='Y'?'Y':'N'
				,'doc_healty': $("input[name=doc_healty]:checked").val()=='Y'?'Y':'N'
				
				,'tele_parent_1':$("#txtTelephone_Parent1").val()
				,'tele_parent_2':$("#txtTelephone_Parent2").val()
				,'tele_parent_3':$("#txtTelephone_Parent3").val()
				};
			
			$("#popupSave").modal('show');
		}
		
		 $("input").jqBootstrapValidation("destroy");
	});		
	$("#btnConfirm").click(function(){
	 
		$.ajax({
				type: 'POST',
				cache: false,
				url: url,
				data: {"m":"add","info":ar},
				success: function(data){
					$("#popupSave").modal('hide');
					alert('บันทึกข้อมูลเรียบร้อย');
					//clearData();
					//CalcTotal();
					window.location.href = window.location.href;
				}
			}); 
	});
 
	function CommaFormatted(amount){
		 
			var delimiter = ","; // replace comma if desired
			var a = amount.split('.',2)
			var d = a[1];
			var i = parseInt(a[0]);
			if(isNaN(i)) { return ''; }
			var minus = '';
			if(i < 0) { minus = '-'; }
			i = Math.abs(i);
			var n = new String(i);
			var a = [];
			while(n.length > 3)
			{
				var nn = n.substr(n.length-3);
				a.unshift(nn);
				n = n.substr(0,n.length-3);
			}
			if(n.length > 0) { a.unshift(n); }
			n = a.join(delimiter);
			if(d.length < 1) { amount = n; }
			else { amount = n + '.' + d; }
			amount = minus + amount;
			return amount;
		}	
	 
    function clearData(){
		var o = {};
		//get name and value control 
		$( "#imageform" ).find('input[type=text]').each(function(){
			$(this).val('');
		});
		$("#txtOtherpay1").val(0);
		$("#txtOtherpay2").val(0);
		$("#txtStudent_form_fee").val(0);
		$("#txtBus_fee").val(0);
		 
	}
});


